package com.personal.website.lottery.web.service.impl;

import com.personal.website.lottery.web.dao.ArticleMapper;
import com.personal.website.lottery.web.dao.CommentMapper;
import com.personal.website.lottery.web.model.Article;
import com.personal.website.lottery.web.model.ArticleExample;
import com.personal.website.lottery.web.service.ArticleService;
import com.personal.website.lottery.web.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 10171225 on 2016/2/18.
 */
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private CommentService commentService;

    public void insertArticle(Article article) {
        this.articleMapper.insert(article);
    }

    public List<Article> getArticles(int num) {
        ArticleExample articleExample = new ArticleExample();
        articleExample.setLimitLength(num);
        articleExample.setOrderByClause("submit_time desc");

        return this.articleMapper.selectByExample(articleExample);
    }

    public List<Article> getArticles(String text, int num) {
        ArticleExample articleExample = new ArticleExample();
        articleExample.setLimitLength(num);
        articleExample.setOrderByClause("submit_time desc");
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        criteria.andTitleLike(text);
        return this.articleMapper.selectByExample(articleExample);
    }

    public Article selectArticleById(int id) {
        return this.articleMapper.selectByPrimaryKey(id);
    }


    public List<Article> getCommentedArticlesByUserId(int userId, int num) {
        List<Integer> articleIdList = this.commentService.getCommentArticleIds(userId);
        ArticleExample example = new ArticleExample();
        example.createCriteria().andIdIn(articleIdList);
        example.setDistinct(true);
        return this.articleMapper.selectByExample(example);
    }


    public boolean updateArticle(Article article) {
        return this.articleMapper.updateByPrimaryKey(article) == 1;
    }

    public boolean deleteArticle(int id) {
        return this.articleMapper.deleteByPrimaryKey(id) >= 1;
    }

    @Override
    public List<Article> getReviewedArticles(int num) {
        ArticleExample articleExample = new ArticleExample();
        articleExample.setLimitLength(num);
        articleExample.setOrderByClause("submit_time desc");
        articleExample.createCriteria().andIsReviewedEqualTo((byte) 1);

        return this.articleMapper.selectByExample(articleExample);
    }

    @Override
    public List<Article> getUnReviewedArticles(int num) {
        ArticleExample articleExample = new ArticleExample();
        articleExample.setLimitLength(num);
        articleExample.setOrderByClause("submit_time desc");
        articleExample.createCriteria().andIsReviewedEqualTo((byte) 0);

        return this.articleMapper.selectByExample(articleExample);
    }


    public List<Article> getIssuedArticlesByUserId(int userId, int num) {
        ArticleExample articleExample = new ArticleExample();
        articleExample.createCriteria().andUserIdEqualTo(userId);
        articleExample.setOrderByClause("submit_time desc");
        articleExample.setLimitLength(num);
        return this.articleMapper.selectByExample(articleExample);
    }


}
